package com.nuoniu.sibanyun.controller.erp.in;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.UsedGradeRuleBo;
import com.nuoniu.sibanyun.entity.erp.in.InInventoryLocation;
import com.nuoniu.sibanyun.entity.erp.in.InWarehouse;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpAccountingService;
import com.nuoniu.sibanyun.service.erp.IErpGradeRuleService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryLocationService;
import com.nuoniu.sibanyun.service.erp.in.IInWarehouseService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 货位 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/in/inventoryLocation")
@Api(value = "货位管理", tags = "货位管理")
public class InInventoryLocationController extends BaseController {

    @Autowired
    private IInInventoryLocationService locationService;
    @Autowired
    private IInWarehouseService warehouseService;
    @Autowired
    private IErpAccountingService accountingService;
    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IErpGradeRuleService erpGradeRuleService;

    /**
     * 获取货位列表
     * @author 小啊刚
     * @date 2020-09-23
     */
    @GetMapping("pageList")
    @ApiOperation(value = "获取货位列表")
    public HttpResult pageList(Page page, InInventoryLocation location) {
        location.setAccountId(accountService.getCurrentAccountId());
        return HttpResult.ok(locationService.pageList(page, location));
    }
    @GetMapping("pageList1")
    @ApiOperation(value = "获取货位列表")
    public HttpResult pageList1(Page page, InInventoryLocation location) {
        location.setAccountId(accountService.getCurrentAccountId());
        return HttpResult.ok(locationService.pageList1(page, location));
    }
    /**
     * 保物料位
     * @author 小啊刚
     * @date 2020-09-23
     */
    @PostMapping("save")
    @ApiOperation(value = "保物料位")
    public HttpResult save(@RequestBody InInventoryLocation location) {
        boolean isNew = false;
        if(StringUtils.isBlank(location.getName())) {
            throw new NuoNiuException("货位名称不能空");
        }
        if(StringUtils.isBlank(location.getCode())) {
            throw new NuoNiuException("货位编码不能为空");
        }
        if(location.getWarehouseId() == null) {
            throw new NuoNiuException("请选择仓库");
        }

        //判断货位编码规则
        UsedGradeRuleBo usedGradeRuleBo = erpGradeRuleService.checkCode(location.getCode(), null, null, "inventoryLocation");
        location.setGradeRuleLevel(usedGradeRuleBo.getGradeRuleLevel());
        location.setGradeRuleStr(usedGradeRuleBo.getGradeRuleStr());

        UserInfo userInfo = UserThreadLocal.get();
        location.setCompanyId(userInfo.getCompanyId());
        InInventoryLocation inventoryByCode = locationService.getByCode(location);
        InInventoryLocation locationByName = locationService.getByName(location);
        InWarehouse warehouse = warehouseService.getById(location.getWarehouseId());
        location.setWarehouseName(warehouse.getWhName());

        if(location.getId() != null) {
            if(locationByName != null && StringUtils.isBlank(locationByName.getName())) {
                throw new NuoNiuException("货位名称已存在");
            }
            if(inventoryByCode != null && !(StringUtils.isBlank(inventoryByCode.getCode()))) {
                throw new NuoNiuException("货位编码已存在");
            }
            location.setUpdateUserId(userInfo.getUserId());
            location.setUpdateDate(LocalDateTime.now());
        }else {
            isNew = true;
            if(inventoryByCode != null) {
                throw new NuoNiuException("货位编码已存在");
            }
            if(locationByName != null) {
                throw new NuoNiuException("货位名称已存在");
            }
            location.setAccountId(accountService.getCurrentAccountId());
            location.setCreateUserId(userInfo.getUserId());
        }
        locationService.saveOrUpdate(location);
        if(!isNew){
            /**
             * 批量同步 货位名称 和 货位编码
             * @param location
             */
            locationService.batchUpdateFromInInventoryLocation(location);
        }
        return HttpResult.ok();
    }



    /**
     * 删除货位
     * @author 小啊刚
     * @date 2020-09-23
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除货位")
    public HttpResult delete(@RequestBody List<Integer> ids) {

        for(Integer id : ids) {
            locationService.removeById(id);
        }
        return HttpResult.ok();
    }
}
